{% set pypi_name = 'monasca-log-api' %}
{% set source = fetch_source('https://tarballs.openstack.org/monasca-log-api/monasca-log-api-master.tar.gz') %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}

Name:           {{ py2name() }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Summary:        OpenStack Monasca Log Api plugin
License:        {{ license('Apache-2.0') }}
Group:          Development/Languages/Python
URL:            https://wiki.openstack.org/wiki/Monasca
Source0:        {{ source|basename }}
Source1:        %{name}-wsgi.conf
BuildRequires:  fdupes
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('falcon') }}
BuildRequires:  {{ py3('fixtures') }}
BuildRequires:  {{ py3('keystonemiddleware') }}
BuildRequires:  {{ py3('mock') }}
BuildRequires:  {{ py3('monasca-common') }}
BuildRequires:  {{ py3('monasca-statsd') }}
BuildRequires:  {{ py3('oslo.config') }}
BuildRequires:  {{ py3('oslo.context') }}
BuildRequires:  {{ py3('oslo.log') }}
BuildRequires:  {{ py3('oslo.middleware') }}
BuildRequires:  {{ py3('oslo.serialization') }}
BuildRequires:  {{ py3('oslo.utils') }}
BuildRequires:  {{ py3('oslotest') }}
BuildRequires:  {{ py3('pbr') }}
BuildRequires:  {{ py3('simplejson') }}
BuildRequires:  {{ py3('stestr') }}
Requires:       python3-{{ pypi_name }} = %{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  apache2-mod_wsgi-python3
Requires:       apache2-mod_wsgi-python3
%else
BuildRequires:  python3-mod_wsgi
Requires:       python3-mod_wsgi
%endif

%description
monasca-log-api is a RESTful API server receiving logs from the deployed
log agents.

%package -n python3-{{ pypi_name }}
Summary:        OpenStack Monasca Log Api plugin
Group:          Development/Languages/Python
Requires:       {{ py3('eventlet') }}
Requires:       {{ py3('falcon') }}
Requires:       {{ py3('keystonemiddleware') }}
Requires:       {{ py3('monasca-common') }}
Requires:       {{ py3('monasca-statsd') }}
Requires:       {{ py3('oslo.config') }}
Requires:       {{ py3('oslo.context') }}
Requires:       {{ py3('oslo.log') }}
Requires:       {{ py3('oslo.middleware') }}
Requires:       {{ py3('oslo.serialization') }}
Requires:       {{ py3('oslo.utils') }}
Requires:       {{ py3('pbr') }}
BuildArch:      noarch

%description -n python3-{{ pypi_name }}
monasca-log-api is a RESTful API server receiving logs from the deployed
log agents.

%prep
%autosetup -p1 -n {{ pypi_name }}-{{ upstream_version }}
%py_req_cleanup

%build
%py3_build
oslo-config-generator \
  --config-file config-generator/monasca-log-api.conf \
  --output-file etc/monasca/log-api.conf

%install
%py3_install

## directories
install -d -m 755 %{buildroot}%{_sysconfdir}/monasca
install -d -m 750 %{buildroot}%{_var}/log/monasca
install -d -m 755 %{buildroot}%{_var}/lib/monasca-log-api
install -d -m 755 %{buildroot}%{apache_site_dir}

## configuration directory
find %{buildroot}%{_prefix}%{_sysconfdir}/ -type f -name 'log-api-*' -print0 | xargs -0 mv -t %{buildroot}%{_sysconfdir}/monasca/
rm -rf %{buildroot}%{_prefix}%{_sysconfdir}/monasca

# configuration bits
# NOTEtrebskit) file names are hardcoded in monasca-log-api
# should be refactored when Bug#1670277 is resolved
install -D -m 640 etc/monasca/log-api.conf %{buildroot}%{_sysconfdir}/monasca/log-api.conf
install -D -m 640 etc/monasca/log-api-paste.ini %{buildroot}%{_sysconfdir}/monasca/log-api-paste.ini
install -D -m 640 etc/monasca/log-api-logging.conf %{buildroot}%{_sysconfdir}/monasca/log-api-logging.conf

## install apache configuration
install -p -D -m 640 %{SOURCE1} %{buildroot}%{apache_site_dir}/{{ pypi_name }}.conf.sample
sed -e "
    s|__APACHE_NAME__|%{apache_name}|g;
" -i %{buildroot}%{apache_site_dir}/{{ pypi_name }}.conf.sample

# there is extra directory /etc/monasca/monasca created, get rid of it
rm -rf %{_sysconfdir}/monasca/monasca || true

%fdupes %{buildroot}%{python3_sitelib}

%check
find . -type f -name *.pyc -delete
OS_TEST_PATH=./monasca_log_api/tests PYTHONPATH=. python3 -m stestr.cli run

%pre
%openstack_pre_user_group_create monasca-log-api monasca

%postun
if [ $1 -eq 0 ] ; then
  getent passwd monasca-log-api > /dev/null
  if [ "$?" == "0" ] ; then
      userdel monasca-log-api
  fi
fi

%files
%doc README.rst ChangeLog
%{_bindir}/monasca-log-api
%{_bindir}/monasca-log-api-wsgi

%dir %attr(-, root, monasca) %{_sysconfdir}/monasca
%dir %attr(0750, root, monasca) %{_var}/log/monasca
%dir %attr(0750, monasca-log-api, monasca) %{_var}/lib/monasca-log-api

%config(noreplace) %attr(0640, root, monasca) %{_sysconfdir}/monasca/log-api.conf
%config(noreplace) %attr(0640, root, monasca) %{_sysconfdir}/monasca/log-api-logging.conf
%config(noreplace) %attr(0640, root, monasca) %{_sysconfdir}/monasca/log-api-paste.ini
%config(noreplace) %attr(0640, root, monasca) %{apache_site_dir}/{{ pypi_name }}.conf.sample

%files -n python3-{{ pypi_name }}
%license LICENSE
%{python3_sitelib}/monasca_log_api
%{python3_sitelib}/*.egg-info

%changelog
